package ai;

import java.util.ArrayList;
import java.util.List;

public class City {

	private String cityName = "";
	private double latitude;
	private double longitude;
	private double distanceToGoal;
	private boolean visited = false;
	private List<Integer> neighbors = new ArrayList<Integer>();
	
	public City(String cityName) {
		this.cityName = cityName;
	}
	
	public City(String cityName, double latitude, double longitude) {
		this.cityName = cityName;
		this.latitude = latitude;
		this.longitude = longitude;
	}
	
	public City(String cityName, String latitude, String longitude) {
		this.cityName = cityName;
		this.latitude = coordToDecimal(latitude);
		this.longitude = coordToDecimal(longitude);
	}
	
	// change coordinates to decimal (Kevin Wilett)
	public static double coordToDecimal(String s) {
		// System.out.println("string: " + s);
		// not so efficient, but the string is small
		int deg = Integer.valueOf(s.substring(0, 2));
		int min = Integer.valueOf(s.substring(3, 5));
		int sec = Integer.valueOf(s.substring(6, 8));
		
		return deg + min / 60. + sec / 3600.;
	}
	
	public void setCityName(String cityName) {
		this.cityName = cityName;
	}
	
	public String getCityName() {
		return this.cityName;
	}
	
	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}
	
	public void setLatitude(String latitude) {
		this.latitude = coordToDecimal(latitude);
	}
	
	public double getLatitude() {
		return this.latitude;
	}
	
	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}
	
	public void setLongitude(String longitude) {
		this.latitude = coordToDecimal(longitude);
	}
	
	public double getLongitude() {
		return this.longitude;
	}
		// city 1 = current city, city 2 = goal city
	public void setDistanceToGoal(City goalCity) {
		//calculate estimated distance between each city (Elizabeth Murrell)
		double R = 6378.1; // kilometers
		double dLat = (goalCity.getLatitude() - getLatitude()) / 57.2957795;
		double dLon = (goalCity.getLongitude() - getLongitude()) / 57.2957795;
		double lat1 = getLatitude() / 57.2957795;
		double lat2 = goalCity.getLatitude() / 57.2957795;

		double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
		Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
		double d = R * c;
		//convert to miles
		d = d * 0.62137119;
		distanceToGoal = d;
	}
	
	public double getDistanceToGoal() {
		return distanceToGoal;
	}

	public void setVisited() {
		this.visited = true;
	}
	
	public void setNotVisited() {
		this.visited = false;
	}

	public boolean isVisited() {
		return visited;
	}

	public void setNeighbors(List<Integer> neighbors) {
		this.neighbors = neighbors;
	}

	public List<Integer> getNeighbors() {
		return neighbors;
	}
	
	public String toString() {
		String results = "cityName: " + cityName +
		" latitude: " + latitude + " longitude: " +
		longitude + " neighbors: " + neighbors +
		" distanceToGoal: " + distanceToGoal;
		return results;
	}
}
